package com.dhcc.bpm.modules.system.wrapper;

import com.dhcc.bpm.common.cache.BusinessParamsCache;
import com.dhcc.bpm.common.cache.DictCache;
import com.dhcc.bpm.modules.system.entity.BusinessParams;
import com.dhcc.bpm.modules.system.vo.BusinessParamsVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName DictWrapper
 * @Description 包装类,返回视图层所需的字段
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
public class BusinessParamsWrapper extends BaseEntityWrapper<BusinessParams, BusinessParamsVO> {

	public static BusinessParamsWrapper build() {
		return new BusinessParamsWrapper();
	}

	@Override
	public BusinessParamsVO entityVO(BusinessParams dict) {
		BusinessParamsVO dictVO = BeanUtil.copy(dict, BusinessParamsVO.class);
		assert dictVO != null;
		if (Func.isEquals(dict.getParentId(), BladeConstant.TOP_PARENT_ID)) {
			dictVO.setParentName(BladeConstant.TOP_PARENT_NAME);
		} else {
			BusinessParams parent = BusinessParamsCache.getById(dict.getParentId());
			if(parent != null){
				dictVO.setParentName(parent.getDictValue());
			}
		}
		return dictVO;
	}

	public List<INode> listNodeVO(List<BusinessParams> list) {
		List<INode> collect = list.stream().map(dict -> BeanUtil.copy(dict, BusinessParamsVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

}
